<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title">菜单管理</h3>
        <div class="box-tools pull-right">
            <div class="btn-group" role="group">
                <button type="button" class="btn btn-default btn-delete">删除</button>
                <button type="button" class="btn btn-default btn-add">添加</button>
                <button type="button" class="btn btn-default btn-update">修改</button>
            </div>
        </div>
    </div>
    <!-- /.box-header -->
    <div class="box-body no-padding">

        <div class="table-responsive mailbox-messages">
            <table id="menuTable" class="table table-hover table-striped">
                <thead>
                <th data-field="selectItem" data-checkbox="true"></th>
                </thead>
            </table>
        </div>
    </div>
</div>
<!-- jQuery 3 -->
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/dist/js/adminlte.min.js"></script>
<script src="/dist/js/demo.js"></script>
<script src="/bower_components/treegrid/jquery.treegrid.extension.js"></script>
<script src="/bower_components/treegrid/jquery.treegrid.min.js"></script>
<script src="/bower_components/treegrid/tree.table.js"></script>
<script type="text/javascript">

    var colums=[
        {
            field:'selectItem',
            radio:true
        },
        {
            title: '菜单ID',
            field: 'id',
            align: 'center',
            valign: 'middle',
            width:'80px'
        },
        {
            title: '菜单名称',
            field: 'name',
            align: 'center',
            valign: 'middle',
            width:'130px'
        },
        {
            title: '上级菜单',
            field: 'parentName',
            align: 'center',
            valign: 'middle',
            width:'100px'
        },
        {
            title: '类型',
            field: 'type',
            align: 'center',
            valign: 'middle',
            width:'70px',
            formatter:function (item,index) {
                if (item.type==1){
                    return '<span class="label label-success">菜单</span>';
                }else if (item.type==2){
                    return '<span class="label label-warning">按钮</span>';
                }
            }
        },
        {
            title: '排序号',
            field: 'sort',
            align: 'center',
            valign: 'middle',
            sortable:true,
            width:'70px'
        },
        {
            title: '菜单URL',
            field: 'url',
            align: 'center',
            valign: 'middle',
            width:'160px'
        },
        {
            title: '授权标识',
            field: 'permission',
            align: 'center',
            valign: 'middle',
            sortable:false
        }
    ];
    $(function () {
        doGetMenus();
        $(".btn-group").on("click",".btn-delete",doDeleteObject)
            .on("click",".btn-add",doLoadEditUI)
            .on("click",".btn-update",doLoadEditUI)


    })
    function doLoadEditUI() {
        var title;
        if ($(this).hasClass("btn-add")){
            title="添加菜单";
        }else if($(this).hasClass("btn-update")){
            title="修改菜单";
            //获取当前数据
            var rowData=doGetCheckedItem();
            if (!rowData){
                alert("请选择一个");
                return;
            }
            $("#mainID").data("rowData",rowData);
            // console.log($(".content-header").data("rowData"));
        }
        $("#mainID").load("menu/menu_edit",function () {
            $(".box-title").html(title);
        });
    }
    function doGetCheckedItem() {
        var tr=$("tbody input[type='radio']:checked").parents("tr");
        return tr.data("rowData");
    }
    function doDeleteObject() {
        //获取选中的id
        var id=doGetCheckedId();
        if (!id){
            alert("请先选择！");
            return;
        }
        if (!confirm("确认删除吗？"))
            return;
        var url="/menu/doDeleteObjects";
        var params={id:id};
        $.post(url,params,function (result) {
            if(result.state==1){
                alert("删除成功");
                $("tbody input[type='radio']:checked").parents("tr").remove();
            }else {
                alert(result.message);
            }
        })
    }
    function doGetCheckedId() {
        var selections=$("#menuTable").bootstrapTreeTable("getSelections");
        if(selections.length==1){
            return selections[0].id;
        }
    }
    function doGetMenus() {
        var treeTable=new TreeTable('menuTable','/menu/doFindObjects',colums);
        treeTable.init();
    }


</script>
